package com.cloud.weixin.project.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cloud.common.core.model.response.ApiResponses;
import com.cloud.common.core.model.supers.SuperController;
import com.cloud.common.core.utils.LocalDateTimeUtils;
import com.cloud.common.security.annotation.PreAuth;
import com.cloud.weixin.project.model.entity.WxTemplate;
import com.cloud.weixin.project.service.IWxTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 微信模板 前端控制器
 * </p>
 *
 * @author Sam
 * @since 2021-04-27
 */
@Api(tags = {"微信模板"})
@RestController
@RequestMapping("/wxTemplate")
public class WxTemplateRestController extends SuperController {

    @Autowired
    private IWxTemplateService wxTemplateService;

    @PreAuth
    @ApiOperation("查询所有微信模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cursor", value = "当前页数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "微信模板名称 模糊", paramType = "query"),
    })
    @GetMapping
    public ApiResponses<IPage<WxTemplate>> page(@RequestParam(value = "title", required = false) String title) {
        LambdaQueryWrapper<WxTemplate> wrapper = Wrappers.<WxTemplate>lambdaQuery()
                .like(StringUtils.isNotBlank(title), WxTemplate::getTitle, title);
        IPage<WxTemplate> wxTemplateIPage = wxTemplateService.page(this.getPage(), wrapper);
        return success(wxTemplateIPage);
    }

    @PreAuth
    @ApiOperation("单条查询微信模板")
    @GetMapping("/{id}")
    public ApiResponses<WxTemplate> getById(@PathVariable("id") String id) {
        WxTemplate wxTemplate = wxTemplateService.getById(id);
        return success(wxTemplate);
    }

    @PreAuth
    @ApiOperation("同步微信模板")
    @PostMapping("/sync")
    public ApiResponses<Void> sync() {
        wxTemplateService.sync();
        return success();
    }

    @PreAuth
    @ApiOperation("删除微信模板")
    @DeleteMapping("/{id}")
    public ApiResponses<Void> deleteById(@PathVariable("id") String id) {
        wxTemplateService.delWxTemplate(id);
        return success();
    }

    @PreAuth
    @ApiOperation("微信推送")
    @PostMapping("/send")
    public ApiResponses<Void> send(@RequestParam("openId") String openId) {
        Map<String, String> map = new HashMap<>();
        map.put("first", "贷款还款通知");
        map.put("keyword1", "测试用户");
        map.put("keyword2", "5000元");
        map.put("keyword3", "0天");
        map.put("keyword4", "100元");
        map.put("keyword5", LocalDateTimeUtils.getCurrentTime());
        map.put("remark", "HelloWorld为您服务！");
        wxTemplateService.sendWxTempMsg(openId, "VQYqEuaqhUHh5zX6-OAn5jB5m5ANel4TZvlUISNcDww", "", map);
        return success();
    }

}

